package com.cz.modules.cw.service.impl;

import cn.hutool.core.date.DateUtil;
import com.cz.common.utils.ParkingUtil;
import com.cz.common.utils.ParkingUtil;
import org.springframework.stereotype.Service;

import java.text.DecimalFormat;
import java.util.*;


import java.util.Date;
import java.util.Map;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.cz.common.utils.PageUtils;
import com.cz.common.utils.Query;

import com.cz.modules.cw.dao.CwParkingSpaceDao;
import com.cz.modules.cw.entity.CwParkingSpaceEntity;
import com.cz.modules.cw.service.CwParkingSpaceService;


@Service("cwParkingSpaceService")
public class CwParkingSpaceServiceImpl extends ServiceImpl<CwParkingSpaceDao, CwParkingSpaceEntity> implements CwParkingSpaceService {

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<CwParkingSpaceEntity> page = this.page(
                new Query<CwParkingSpaceEntity>().getPage(params),
                new QueryWrapper<CwParkingSpaceEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    public List cwStatisticsHour(String dept, String startTime, String endTime) {
        int cwcount=0;//车位数
        if("南区".equals(dept)){
            Map<String,Integer>  southCountMap=ParkingUtil.southCountMap;
            for (Map.Entry<String, Integer> entry : southCountMap.entrySet()) {
                cwcount +=entry.getValue();
            }
        }else if("北区".equals(dept)){
            Map<String,Integer>  northCountMap=ParkingUtil.northCountMap;
            for (Map.Entry<String, Integer> entry : northCountMap.entrySet()) {
                cwcount +=entry.getValue();
            }
        }
        //该时间段内的数据
        List<CwParkingSpaceEntity> cwParkingSpaceList=this.list(
                new QueryWrapper<CwParkingSpaceEntity>()
                .eq("area",dept)
                .ge("data_time",startTime)
                .le("data_time",endTime)
        );

        Long start= DateUtil.parse(startTime).getTime();
        Long end=DateUtil.parse(endTime).getTime();
        Calendar calendar=Calendar.getInstance();
        calendar.setTime(new Date(start));
        List<Long> timeList=new ArrayList<>();
        for (Long i = start; i <=end ; i=calendar.getTimeInMillis()) {
            calendar.add(Calendar.HOUR,1);
            timeList.add(calendar.getTimeInMillis());
        }
        List<String> cwzyl=new ArrayList<>();
        Long s=start;
        List<String> timeStrList=new ArrayList<>();
        for (int i = 0; i <timeList.size() ; i++) {
            timeStrList.add(DateUtil.format(new Date(timeList.get(i)),"HH:mm"));
            int  count=0;
            int size=0;
            for (CwParkingSpaceEntity cwParkingSpace:cwParkingSpaceList) {
                Long crossTime=cwParkingSpace.getDataTime().getTime();
                if(crossTime>=s && crossTime<timeList.get(i)){
                    count +=cwParkingSpace.getBackTrunk()+cwParkingSpace.getBuss()+cwParkingSpace.getCar()+cwParkingSpace.getFrontTrunk()+cwParkingSpace.getLongTrunk();
                    size++;
                }
            }
            if(size ==0){
                size =1;
            }
            s=timeList.get(i);
            int avg= count/size;
            DecimalFormat df = new DecimalFormat("0.0");
            String zyl= df.format((float)avg/cwcount*100);
            cwzyl.add(zyl);
        }

        List list=new ArrayList();
        list.add(timeStrList);//放入时间
        list.add(cwzyl);//车位占用率
        return list;
    }

    @Override
    public List cwStatisticsDay(String dept, String startTime, String endTime) {
        int cwcount=0;//车位数
        if("南区".equals(dept)){
            Map<String,Integer>  southCountMap=ParkingUtil.southCountMap;
            for (Map.Entry<String, Integer> entry : southCountMap.entrySet()) {
                cwcount +=entry.getValue();
            }
        }else if("北区".equals(dept)){
            Map<String,Integer>  northCountMap=ParkingUtil.northCountMap;
            for (Map.Entry<String, Integer> entry : northCountMap.entrySet()) {
                cwcount +=entry.getValue();
            }
        }
        List<CwParkingSpaceEntity> cwParkingSpaceList=this.list(
                new QueryWrapper<CwParkingSpaceEntity>()
                        .eq("area",dept)
                        .ge("data_time",startTime)
                        .le("data_time",endTime)
        );
        Long start= DateUtil.parse(startTime).getTime();
        Long end=DateUtil.parse(endTime).getTime();
        Calendar calendar=Calendar.getInstance();
        calendar.setTime(new Date(start));
        List<Long> timeList=new ArrayList<>();
        for (Long i = start; i <=end ; i=calendar.getTimeInMillis()) {
            timeList.add(calendar.getTimeInMillis());
            calendar.add(Calendar.DAY_OF_MONTH,1);
        }
        List<String> timeStrList=new ArrayList<>();
        List<String> cwzyl=new ArrayList<>();
        for (Long ltime:timeList) {
            timeStrList.add(DateUtil.format(new Date(ltime),"MM-dd"));
        }
        for (String time:timeStrList) {
            int  count=0;
            int size=0;
            for (CwParkingSpaceEntity cwParkingSpace:cwParkingSpaceList) {
                String tt=DateUtil.format(cwParkingSpace.getDataTime(),"MM-dd");
                if(tt.equals(time)){
                    count +=cwParkingSpace.getBackTrunk()+cwParkingSpace.getBuss()+cwParkingSpace.getCar()+cwParkingSpace.getFrontTrunk()+cwParkingSpace.getLongTrunk();
                    size++;
                }
            }
            if(size==0){
                size=1;
            }
            int avg= count/size;
            DecimalFormat df = new DecimalFormat("0.0");
            String zyl= df.format((float)avg/cwcount*100);
            cwzyl.add(zyl);
        }

        List list=new ArrayList();
        list.add(timeStrList);//放入时间
        list.add(cwzyl);//车位占用率
        return list;
    }

    @Override
    public Map<String,List> cwCountBytime(String startTime, String endTime) {
        int sbcwcount=0;//南区da车位数
        int sscwcount=0;//南区da车位数
        int nbcwcount=0;//北区车位数
        int nscwcount=0;//北区车位数
        sbcwcount = ParkingUtil.southCountMap.get("超长停车区")+ParkingUtil.southCountMap.get("前广场大车区")+ParkingUtil.southCountMap.get("后广场货车区");
        sscwcount = ParkingUtil.southCountMap.get("小车区");
        nbcwcount = ParkingUtil.northCountMap.get("超长停车区")+ParkingUtil.northCountMap.get("前广场货车区")+ParkingUtil.northCountMap.get("前广场客车区")+ParkingUtil.northCountMap.get("后广场货车区");
        nscwcount = ParkingUtil.northCountMap.get("小车区");
        //该时间段内的数据
        List<CwParkingSpaceEntity> cwParkingSpaceList=this.list(
                new QueryWrapper<CwParkingSpaceEntity>()
                        .ge("data_time",startTime)
                        .le("data_time",endTime)
        );
        int totalbcount=sbcwcount+nbcwcount;//南北区da车位总数
        int totalscount=sscwcount+nscwcount;//南北区xiao车位总数
        int soubcount=0;//南区占用大车位数
        int souscount=0;//南区占用小车位数
        int norbcount=0;//北区占用大车位数
        int norscount=0;//北区占用小车位数
        int sousize=0;
        int norsize=0;
        for (CwParkingSpaceEntity cwParkingSpace:cwParkingSpaceList) {
            if("南区".equals(cwParkingSpace.getArea())){
                soubcount +=cwParkingSpace.getBackTrunk()+cwParkingSpace.getBuss()+cwParkingSpace.getFrontTrunk()+cwParkingSpace.getLongTrunk();
                souscount +=cwParkingSpace.getCar();
                sousize++;
            }else if("北区".equals(cwParkingSpace.getArea())){
                norbcount +=cwParkingSpace.getBackTrunk()+cwParkingSpace.getBuss()+cwParkingSpace.getFrontTrunk()+cwParkingSpace.getLongTrunk();
                norscount +=cwParkingSpace.getCar();
                norsize++;
            }
        }
        if(sousize==0){
            sousize=1;
        }
        if(norsize==0){
            norsize=1;
        }
        int soubavg=soubcount/sousize;//南区大车平均车位占用数
        int sousavg=souscount/sousize;//南区小车平均车位占用数
        int norbavg=norbcount/norsize;
        int norsavg=norscount/norsize;
        int totalbavg=soubavg+norbavg;//总占用da车位数
        int totalsavg=sousavg+norsavg;//总占用xiao车位数
        int soubsycw=sbcwcount-soubavg;//南区da剩余车位
        int soussycw=sscwcount-sousavg;//南区xiao剩余车位
        int norbsycw=nbcwcount-norbavg;
        int norssycw=nscwcount-norsavg;
        int totalbsycw=soubsycw+norbsycw;//总剩余da车位
        int totalssycw=soussycw+norssycw;//总剩余xiao车位
        DecimalFormat df = new DecimalFormat("0.0");
        String soubzyl= df.format((float)soubavg/sbcwcount*100);//南区da占用率
        String souszyl= df.format((float)sousavg/sscwcount*100);//南区xiao占用率
        String norbzyl= df.format((float)norbavg/nbcwcount*100);//北区da占用率
        String norszyl= df.format((float)norsavg/nscwcount*100);//北区da占用率
        String totalbzyl= df.format((float)totalbavg/totalbcount*100);//总占用率
        String totalszyl= df.format((float)totalsavg/totalscount*100);//总占用率

        Map<String,List> map=new HashMap<>();
        List list=new ArrayList();
        list.add(sbcwcount+sscwcount);
        list.add(soubsycw);
        list.add(soubzyl);
        list.add(soussycw);
        list.add(souszyl);
        map.put("南区",list);
        List list1=new ArrayList();
        list1.add(nbcwcount+nscwcount);
        list1.add(norbsycw);
        list1.add(norbzyl);
        list1.add(norssycw);
        list1.add(norszyl);
        map.put("北区",list1);
        List list2=new ArrayList();
        list2.add(totalbcount+totalscount);
        list2.add(totalbsycw);
        list2.add(totalbzyl);
        list2.add(totalssycw);
        list2.add(totalszyl);
        map.put("总额",list2);
        return map;
    }

    @Override
    public void saveParkingSpaceUse() {
        Date d = new Date();
        //存储南区车位使用情况
        CwParkingSpaceEntity cps = new CwParkingSpaceEntity();
        int p1u = 0,p2u=0,p3u=0,p4u=0,p5u=0;//每区使用车位数
        cps.setArea("南区");
        Map<String,Integer> map1 = ParkingUtil.southMap.get("超长停车区");
        if(map1!=null) {
            for (Map.Entry<String, Integer> en : map1.entrySet()) {
                p1u += en.getValue();
            }
        }
        cps.setLongTrunk(p1u);
        Map<String,Integer> map2 = ParkingUtil.southMap.get("前广场大车区");
        if(map2!=null) {
            for (Map.Entry<String, Integer> en : map2.entrySet()) {
                p2u += en.getValue();
            }
        }
        cps.setFrontTrunk(p2u);
        cps.setBuss(0);
        Map<String,Integer> map4 = ParkingUtil.southMap.get("后广场货车区");
        if(map4!=null) {
            for (Map.Entry<String, Integer> en : map4.entrySet()) {
                p4u += en.getValue();
            }
        }
        cps.setBackTrunk(p4u);
        Map<String,Integer> map5 = ParkingUtil.southMap.get("小车区");
        if(map5!=null) {
            for (Map.Entry<String, Integer> en : map5.entrySet()) {
                p5u += en.getValue();
            }
        }
        cps.setCar(p5u);
        cps.setDataTime(d);
        this.save(cps);
        //存储北区车位使用情况
        CwParkingSpaceEntity cpsN = new CwParkingSpaceEntity();
        cpsN.setArea("北区");
        cpsN.setDataTime(d);
        int p6u = 0,p7u=0,p8u=0,p9u=0,p10u=0;//每区使用车位数
        Map<String,Integer> map6 = ParkingUtil.northMap.get("超长停车区");
        if(map6!=null) {
            for (Map.Entry<String, Integer> en : map6.entrySet()) {
                p6u += en.getValue();
            }
        }
        cpsN.setLongTrunk(p6u);
        Map<String,Integer> map7 = ParkingUtil.northMap.get("前广场货车区");
        if(map7!=null) {
            for (Map.Entry<String, Integer> en : map7.entrySet()) {
                p7u += en.getValue();
            }
        }
        cpsN.setFrontTrunk(p7u);
        Map<String,Integer> map8 = ParkingUtil.northMap.get("前广场客车区");
        if(map8!=null) {
            for (Map.Entry<String, Integer> en : map8.entrySet()) {
                p8u += en.getValue();
            }
        }
        cpsN.setBuss(p8u);
        Map<String,Integer> map9 = ParkingUtil.northMap.get("后广场货车区");
        if(map9!=null) {
            for (Map.Entry<String, Integer> en : map9.entrySet()) {
                p9u += en.getValue();
            }
        }
        cpsN.setBackTrunk(p9u);
        Map<String,Integer> map10 = ParkingUtil.northMap.get("小车区");
        if(map10!=null) {
            for (Map.Entry<String, Integer> en : map10.entrySet()) {
                p10u += en.getValue();
            }
        }
        cpsN.setCar(p10u);
        this.save(cpsN);
    }

}
